<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../js/axios.min.js"></script>
</head>

<body>
    <h1>修改</h1>

    <input type="text" class="pname-ipt" />
    <input type="text" class="pprice-ipt" />
    <input type="text" class="pimg-ipt" />
    <textarea name="" id="" cols="30" rows="10" class="pdesc-ipt"></textarea>

    <button class="save-btn" onclick="updateProduct()">保存</button>
    <script>
        location.search;
        console.log(location.search);
        let str = location.search.slice(1);
        console.log(str);
        let arr = str.split("&");
        console.log(arr);
        let pid = null;
        arr.forEach((v) => {
            v;
            let tempArr = v.split("="); //['id','232323']
            if (tempArr[0] == "id") {
                pid = tempArr[1];
            }
        });
        console.log(pid);

        let detailAPI = "http://jx.xuzhixiang.top/ap/api/detail.php";
        let params = {
            id: pid
        };
        axios.get(detailAPI, {
            params
        }).then((res) => {
            console.log(res.data);
            let pObj = res.data.data;
            console.log(pObj);
            document.querySelector(".pname-ipt").value = pObj.pname;
            document.querySelector(".pprice-ipt").value = pObj.pprice;
            document.querySelector(".pimg-ipt").value = pObj.pimg;
            document.querySelector(".pdesc-ipt").value = pObj.pdesc;
        });

        // 修改事件
        async function updateProduct() {
            console.log(111);
            /*  接口地址：    http://jx.xuzhixiang.top/ap/api/goods/goods-update.php
                接口请求方式：get
                接口参数：

                  pid（必选）商品的id
                  pname（必选）商品
                  pprice（必选）商品
                  pdesc（必选）商品
                  使用方式 */
            console.log(pid); //全局变量 pid

            let pname = document.querySelector(".pname-ipt").value;
            let pprice = document.querySelector(".pprice-ipt").value;
            let pimg = document.querySelector(".pimg-ipt").value;
            let pdesc = document.querySelector(".pdesc-ipt").value;
            let updateProductAPI =
                "http://jx.xuzhixiang.top/ap/api/goods/goods-update.php";
            let params = {
                pname,
                pprice,
                pimg,
                pdesc,
                pid
            };
            console.log(params);
            // 解构赋值 获取 对象的data属性
            let {
                data
            } = await axios.get(updateProductAPI, {
                params
            });
            console.log(data);
            location.href = 'publish.html';
        }
    </script>
</body>

</html>